<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 交换一次的先前排列 -->
    <script>
      var prevPermOpt1 = function (A) {
        let len = A.length
        for (let i = len - 2; i >= 0; i--) {
          let maxId = -1,
            maxi = -1
          // 找到所有小于A[i]的值中的最大值即可满足交换条件
          for (let j = i + 1; j < len; j++) {
            if (A[i] > A[j]) {
              if (maxi < A[j]) {
                maxId = j
                maxi = A[j]
              }
            }
          }
          if (maxId === -1) continue
          else {
            ;[A[i], A[maxId]] = [A[maxId], A[i]]
            break
          }
        }
        return A
      }
      console.log(prevPermOpt1([3, 2, 1]))
    </script>
  </body>
</html>
